package com.tancire.web.controller.platform.system;

import com.tancire.common.core.domain.AjaxResult;
import com.tancire.file.bean.SaveFileBean;
import com.tancire.file.service.ISysFileService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统文件上传、展示、下载控制器
 */
@Controller
@RequestMapping("/file")
public class SysFileController {

    @Resource
    private ISysFileService sysFileService;

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "auth", required = false) String auth) {
        try {
            SaveFileBean saveFileBean = sysFileService.uploadFile(file, auth);
            return AjaxResult.success(saveFileBean);
        } catch (Exception e) {
            return AjaxResult.error("上传文件异常:" + e.getMessage());
        }
    }

    /**
     * 上传
     *
     * @param path
     * @return
     */
    @GetMapping("/preview")
    @ResponseBody
    public AjaxResult preview(HttpServletResponse res, @RequestParam("path") String path) {
        try {
            sysFileService.previewFile(res, path);
        } catch (Exception e) {
            // 异常不处理
        }
        return AjaxResult.error("获取图片失败");
    }

}
